class Solution {
public:
    string reverseStr(string s, int k) {
        string str;
        int length=s.length();
        str.resize(length,'0');
        int i=0;
        int j=0;
        int a=0;
        while(i<length){
            if((i+1)%(2*k)==0){
                j=0;
                while(j<k){
                    str[i-j]=s[i-j];
                    str[i-j-k]=s[i+1-2*k+j];
                    j++;
                }
                a=i+1;
            }
            i++;
        }
        if(length-a<=k&&length-a>0){
            j=1;
            while(length-a>0){
                str[a]=s[length-j];
                j++;
                a++;
            }
        }
        if(length-a<=2*k&&length-a>k){
            j=0;
            while(j<(i-a-k)){
                str[i-j-1]=s[i-1-j];
                j++;
            }
            j=0;
            while(j<k){
                str[a]=s[a+k-1-2*j];
                a++;
                j++;
            }
        }
        return str;
    }
};
